package com.onlinevideo.entity;

import java.util.List;

public class PageBean<T> {

    //当前页，从请求中传入
    private Integer pageNo;

    //每页显示的数据条数
    private Integer pageSize;

    //总的记录数据。查询数据库得出
    private Integer totalCount;

    //总页数，通过计算总记录数（totalCount）和每页显示的数据条数计算得出
    private Integer totalPage;

    //开始索引数据-》从数据库的第几行数据开始拿，有了开始索引位置（startIndex）和每页显示的数据条数（pageSize）
    private Integer startIndex;

    //将每页要显示的数据放在list集合中
    private List<T> data ;

    //每页显示的页数，比如在页面上显示1，2，3，4，5页，start就为1，end就为5，通过计算得出。
    private Integer start ;

    private Integer end ;

    public PageBean(Integer pageNo, Integer pageSize, Integer totalCount) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalCount = totalCount;

        //总页数 totalCount
        if(totalCount%pageSize==0){
            //整除，所以每页显示pageSize条数据，没有多余一页要显示少于pageSize条数据
            this.totalPage = totalCount/pageSize;
        }else{
            //不整除，就要再加一页，来显示多余的数据。
            this.totalPage = totalCount/pageSize + 1;
        }
        this.startIndex = (pageNo-1)*pageSize;
        this.start = 1 ;
        this.end = 5 ;
        //显示页数的算法
        //显示5页
        if(totalPage<=5){
            //总页数都小于5
            this.end = this.totalPage ;
        }else{
            this.start = pageNo - 2 ;
            this.end = pageNo + 2;
            if(start<0){
                this.start = 1 ;
                this.end = 5 ;
            }
            if(end>this.totalPage){
                this.end = totalPage ;
                this.start = end - 5 ;
            }
        }
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(Integer startIndex) {
        this.startIndex = startIndex;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Integer getEnd() {
        return end;
    }

    public void setEnd(Integer end) {
        this.end = end;
    }

    @Override
    public String toString() {
        return "PageBean{" +
                "pageNo=" + pageNo +
                ", pageSize=" + pageSize +
                ", totalCount=" + totalCount +
                ", totalPage=" + totalPage +
                ", startIndex=" + startIndex +
                ", data=" + data +
                ", start=" + start +
                ", end=" + end +
                '}';
    }
}
